{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:05.777290Z",
     "start_time": "2020-08-06T06:45:05.767293Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from datetime import datetime\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:07.492147Z",
     "start_time": "2020-08-06T06:45:07.257027Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>order_time</th>\n",
       "      <th>order_products</th>\n",
       "      <th>order_money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>19970101</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>19970112</td>\n",
       "      <td>1</td>\n",
       "      <td>12.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>19970112</td>\n",
       "      <td>5</td>\n",
       "      <td>77.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>19970102</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3</td>\n",
       "      <td>19970330</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  order_time  order_products  order_money\n",
       "0        1    19970101               1        11.77\n",
       "1        2    19970112               1        12.00\n",
       "2        2    19970112               5        77.00\n",
       "3        3    19970102               2        20.76\n",
       "4        3    19970330               2        20.76"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "columns=['user_id','order_time','order_products','order_money']\n",
    "data=pd.read_table('cdnow5658/CDNOW.txt',sep='\\s+',names=columns)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:08.373353Z",
     "start_time": "2020-08-06T06:45:08.353368Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 69659 entries, 0 to 69658\n",
      "Data columns (total 4 columns):\n",
      "user_id           69659 non-null int64\n",
      "order_time        69659 non-null int64\n",
      "order_products    69659 non-null int64\n",
      "order_money       69659 non-null float64\n",
      "dtypes: float64(1), int64(3)\n",
      "memory usage: 2.1 MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  数据探索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:09.659680Z",
     "start_time": "2020-08-06T06:45:09.640691Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "user_id           0\n",
       "order_time        0\n",
       "order_products    0\n",
       "order_money       0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:10.344258Z",
     "start_time": "2020-08-06T06:45:10.281297Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>order_time</th>\n",
       "      <th>order_products</th>\n",
       "      <th>order_money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>69659.000000</td>\n",
       "      <td>6.965900e+04</td>\n",
       "      <td>69659.000000</td>\n",
       "      <td>69659.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>11470.854592</td>\n",
       "      <td>1.997228e+07</td>\n",
       "      <td>2.410040</td>\n",
       "      <td>35.893648</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>6819.904848</td>\n",
       "      <td>3.837735e+03</td>\n",
       "      <td>2.333924</td>\n",
       "      <td>36.281942</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.997010e+07</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>5506.000000</td>\n",
       "      <td>1.997022e+07</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>14.490000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>11410.000000</td>\n",
       "      <td>1.997042e+07</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>25.980000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>17273.000000</td>\n",
       "      <td>1.997111e+07</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>43.700000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>23570.000000</td>\n",
       "      <td>1.998063e+07</td>\n",
       "      <td>99.000000</td>\n",
       "      <td>1286.010000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            user_id    order_time  order_products   order_money\n",
       "count  69659.000000  6.965900e+04    69659.000000  69659.000000\n",
       "mean   11470.854592  1.997228e+07        2.410040     35.893648\n",
       "std     6819.904848  3.837735e+03        2.333924     36.281942\n",
       "min        1.000000  1.997010e+07        1.000000      0.000000\n",
       "25%     5506.000000  1.997022e+07        1.000000     14.490000\n",
       "50%    11410.000000  1.997042e+07        2.000000     25.980000\n",
       "75%    17273.000000  1.997111e+07        3.000000     43.700000\n",
       "max    23570.000000  1.998063e+07       99.000000   1286.010000"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:11.480751Z",
     "start_time": "2020-08-06T06:45:11.453765Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "255"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(data.duplicated()) # 计算重复值得个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:12.010503Z",
     "start_time": "2020-08-06T06:45:11.977523Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(69404, 4)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除重复值\n",
    "data= data.drop_duplicates(keep='first',inplace=False)\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:12.583440Z",
     "start_time": "2020-08-06T06:45:12.543461Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>order_time</th>\n",
       "      <th>order_products</th>\n",
       "      <th>order_money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>80.000000</td>\n",
       "      <td>8.000000e+01</td>\n",
       "      <td>80.0</td>\n",
       "      <td>80.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>10330.437500</td>\n",
       "      <td>1.997108e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>7155.357265</td>\n",
       "      <td>2.882384e+03</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>455.000000</td>\n",
       "      <td>1.997010e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>3799.250000</td>\n",
       "      <td>1.997012e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>10014.000000</td>\n",
       "      <td>1.997021e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>16183.250000</td>\n",
       "      <td>1.997031e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>23157.000000</td>\n",
       "      <td>1.998042e+07</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            user_id    order_time  order_products  order_money\n",
       "count     80.000000  8.000000e+01            80.0         80.0\n",
       "mean   10330.437500  1.997108e+07             1.0          0.0\n",
       "std     7155.357265  2.882384e+03             0.0          0.0\n",
       "min      455.000000  1.997010e+07             1.0          0.0\n",
       "25%     3799.250000  1.997012e+07             1.0          0.0\n",
       "50%    10014.000000  1.997021e+07             1.0          0.0\n",
       "75%    16183.250000  1.997031e+07             1.0          0.0\n",
       "max    23157.000000  1.998042e+07             1.0          0.0"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查异常值 : 订单金额=0\n",
    "except1 = data[data['order_money']==0]\n",
    "except1.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 判断：金额为0的订单共80个，产品数为1，可能是免费活动，参与免费获得的客户不具有明显价值，需予以剔除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:13.658211Z",
     "start_time": "2020-08-06T06:45:13.641224Z"
    }
   },
   "outputs": [],
   "source": [
    "data= data.drop(index=(data.loc[(data['order_money']==0)].index))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:14.308148Z",
     "start_time": "2020-08-06T06:45:14.204207Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>order_time</th>\n",
       "      <th>order_products</th>\n",
       "      <th>order_money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>1</td>\n",
       "      <td>12.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>5</td>\n",
       "      <td>77.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>1997-01-02</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3</td>\n",
       "      <td>1997-03-30</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id order_time  order_products  order_money\n",
       "0        1 1997-01-01               1        11.77\n",
       "1        2 1997-01-12               1        12.00\n",
       "2        2 1997-01-12               5        77.00\n",
       "3        3 1997-01-02               2        20.76\n",
       "4        3 1997-03-30               2        20.76"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将日期转换我datetime 类型\n",
    "data.loc[:,'order_time']=pd.to_datetime(data['order_time'],format='%Y%m%d')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:14.784874Z",
     "start_time": "2020-08-06T06:45:14.768883Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count                   69324\n",
       "unique                    546\n",
       "top       1997-02-24 00:00:00\n",
       "freq                      502\n",
       "first     1997-01-01 00:00:00\n",
       "last      1998-06-30 00:00:00\n",
       "Name: order_time, dtype: object"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.order_time.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 结论: 数据集时间及集中在1997年初到1998 年中旬:基于此将观察日期定义为1998-06-30"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">RFM定义: R 最近一次的下单时间; F 购买频率 ; M 最近一次订单金额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:16.475163Z",
     "start_time": "2020-08-06T06:45:16.453173Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>order_time</th>\n",
       "      <th>order_products</th>\n",
       "      <th>order_money</th>\n",
       "      <th>order_interval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "      <td>545 days</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>1</td>\n",
       "      <td>12.00</td>\n",
       "      <td>534 days</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>5</td>\n",
       "      <td>77.00</td>\n",
       "      <td>534 days</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>1997-01-02</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "      <td>544 days</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3</td>\n",
       "      <td>1997-03-30</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "      <td>457 days</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id order_time  order_products  order_money order_interval\n",
       "0        1 1997-01-01               1        11.77       545 days\n",
       "1        2 1997-01-12               1        12.00       534 days\n",
       "2        2 1997-01-12               5        77.00       534 days\n",
       "3        3 1997-01-02               2        20.76       544 days\n",
       "4        3 1997-03-30               2        20.76       457 days"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成最近一次下单时间\n",
    "data['order_interval']=pd.to_datetime('1998-06-30')-data['order_time']\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:17.139068Z",
     "start_time": "2020-08-06T06:45:17.079103Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>order_products</th>\n",
       "      <th>order_money</th>\n",
       "      <th>order_interval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>69324.000000</td>\n",
       "      <td>69324.000000</td>\n",
       "      <td>69324.000000</td>\n",
       "      <td>69324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>11470.227569</td>\n",
       "      <td>2.416191</td>\n",
       "      <td>36.004598</td>\n",
       "      <td>362 days 04:30:07.166349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>6813.909552</td>\n",
       "      <td>2.337382</td>\n",
       "      <td>36.318874</td>\n",
       "      <td>159 days 11:56:32.191509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.630000</td>\n",
       "      <td>0 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>5509.750000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>14.490000</td>\n",
       "      <td>235 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>11414.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>25.980000</td>\n",
       "      <td>433 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>17262.250000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>43.730000</td>\n",
       "      <td>493 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>23570.000000</td>\n",
       "      <td>99.000000</td>\n",
       "      <td>1286.010000</td>\n",
       "      <td>545 days 00:00:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            user_id  order_products   order_money            order_interval\n",
       "count  69324.000000    69324.000000  69324.000000                     69324\n",
       "mean   11470.227569        2.416191     36.004598  362 days 04:30:07.166349\n",
       "std     6813.909552        2.337382     36.318874  159 days 11:56:32.191509\n",
       "min        1.000000        1.000000      1.630000           0 days 00:00:00\n",
       "25%     5509.750000        1.000000     14.490000         235 days 00:00:00\n",
       "50%    11414.000000        2.000000     25.980000         433 days 00:00:00\n",
       "75%    17262.250000        3.000000     43.730000         493 days 00:00:00\n",
       "max    23570.000000       99.000000   1286.010000         545 days 00:00:00"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:18.659725Z",
     "start_time": "2020-08-06T06:45:17.677150Z"
    }
   },
   "outputs": [],
   "source": [
    "# 去掉日期间隔days\n",
    "data['order_interval'] = data['order_interval'].apply(lambda x:x.days)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:18.721528Z",
     "start_time": "2020-08-06T06:45:18.702539Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>order_time</th>\n",
       "      <th>order_products</th>\n",
       "      <th>order_money</th>\n",
       "      <th>order_interval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "      <td>545</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>1</td>\n",
       "      <td>12.00</td>\n",
       "      <td>534</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>5</td>\n",
       "      <td>77.00</td>\n",
       "      <td>534</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>1997-01-02</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "      <td>544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3</td>\n",
       "      <td>1997-03-30</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "      <td>457</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id order_time  order_products  order_money  order_interval\n",
       "0        1 1997-01-01               1        11.77             545\n",
       "1        2 1997-01-12               1        12.00             534\n",
       "2        2 1997-01-12               5        77.00             534\n",
       "3        3 1997-01-02               2        20.76             544\n",
       "4        3 1997-03-30               2        20.76             457"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:18.950398Z",
     "start_time": "2020-08-06T06:45:18.893429Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>R</th>\n",
       "      <th>F</th>\n",
       "      <th>M</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>545</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>534</td>\n",
       "      <td>2</td>\n",
       "      <td>89.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>33</td>\n",
       "      <td>6</td>\n",
       "      <td>156.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>200</td>\n",
       "      <td>4</td>\n",
       "      <td>100.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>178</td>\n",
       "      <td>11</td>\n",
       "      <td>385.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>545</td>\n",
       "      <td>1</td>\n",
       "      <td>20.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>100</td>\n",
       "      <td>3</td>\n",
       "      <td>264.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>93</td>\n",
       "      <td>8</td>\n",
       "      <td>197.66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>22</td>\n",
       "      <td>3</td>\n",
       "      <td>95.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>525</td>\n",
       "      <td>1</td>\n",
       "      <td>39.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>11</td>\n",
       "      <td>130</td>\n",
       "      <td>4</td>\n",
       "      <td>58.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>545</td>\n",
       "      <td>1</td>\n",
       "      <td>57.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>13</td>\n",
       "      <td>545</td>\n",
       "      <td>1</td>\n",
       "      <td>72.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>14</td>\n",
       "      <td>545</td>\n",
       "      <td>1</td>\n",
       "      <td>29.92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>15</td>\n",
       "      <td>545</td>\n",
       "      <td>1</td>\n",
       "      <td>52.87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>293</td>\n",
       "      <td>4</td>\n",
       "      <td>79.87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>17</td>\n",
       "      <td>545</td>\n",
       "      <td>1</td>\n",
       "      <td>73.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18</td>\n",
       "      <td>542</td>\n",
       "      <td>1</td>\n",
       "      <td>14.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>19</td>\n",
       "      <td>385</td>\n",
       "      <td>2</td>\n",
       "      <td>175.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20</td>\n",
       "      <td>528</td>\n",
       "      <td>2</td>\n",
       "      <td>653.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>21</td>\n",
       "      <td>533</td>\n",
       "      <td>2</td>\n",
       "      <td>75.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>22</td>\n",
       "      <td>545</td>\n",
       "      <td>1</td>\n",
       "      <td>14.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>23</td>\n",
       "      <td>545</td>\n",
       "      <td>1</td>\n",
       "      <td>24.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>24</td>\n",
       "      <td>161</td>\n",
       "      <td>2</td>\n",
       "      <td>57.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>25</td>\n",
       "      <td>22</td>\n",
       "      <td>8</td>\n",
       "      <td>137.53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>26</td>\n",
       "      <td>520</td>\n",
       "      <td>2</td>\n",
       "      <td>102.69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>27</td>\n",
       "      <td>534</td>\n",
       "      <td>2</td>\n",
       "      <td>135.87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>28</td>\n",
       "      <td>479</td>\n",
       "      <td>3</td>\n",
       "      <td>90.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>29</td>\n",
       "      <td>65</td>\n",
       "      <td>12</td>\n",
       "      <td>435.81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>30</td>\n",
       "      <td>501</td>\n",
       "      <td>2</td>\n",
       "      <td>28.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23472</th>\n",
       "      <td>23541</td>\n",
       "      <td>454</td>\n",
       "      <td>2</td>\n",
       "      <td>57.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23473</th>\n",
       "      <td>23542</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>77.43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23474</th>\n",
       "      <td>23543</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>50.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23475</th>\n",
       "      <td>23544</td>\n",
       "      <td>157</td>\n",
       "      <td>3</td>\n",
       "      <td>134.63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23476</th>\n",
       "      <td>23545</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>24.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23477</th>\n",
       "      <td>23546</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>13.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23478</th>\n",
       "      <td>23547</td>\n",
       "      <td>449</td>\n",
       "      <td>2</td>\n",
       "      <td>23.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23479</th>\n",
       "      <td>23548</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>23.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23480</th>\n",
       "      <td>23549</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>27.13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23481</th>\n",
       "      <td>23550</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>25.28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23482</th>\n",
       "      <td>23551</td>\n",
       "      <td>292</td>\n",
       "      <td>6</td>\n",
       "      <td>264.63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23483</th>\n",
       "      <td>23552</td>\n",
       "      <td>453</td>\n",
       "      <td>2</td>\n",
       "      <td>49.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23484</th>\n",
       "      <td>23553</td>\n",
       "      <td>459</td>\n",
       "      <td>2</td>\n",
       "      <td>98.58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23485</th>\n",
       "      <td>23554</td>\n",
       "      <td>149</td>\n",
       "      <td>2</td>\n",
       "      <td>36.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23486</th>\n",
       "      <td>23555</td>\n",
       "      <td>20</td>\n",
       "      <td>5</td>\n",
       "      <td>189.18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23487</th>\n",
       "      <td>23556</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>203.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23488</th>\n",
       "      <td>23557</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>14.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23489</th>\n",
       "      <td>23558</td>\n",
       "      <td>125</td>\n",
       "      <td>4</td>\n",
       "      <td>145.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23490</th>\n",
       "      <td>23559</td>\n",
       "      <td>368</td>\n",
       "      <td>3</td>\n",
       "      <td>111.65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23491</th>\n",
       "      <td>23560</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>18.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23492</th>\n",
       "      <td>23561</td>\n",
       "      <td>32</td>\n",
       "      <td>3</td>\n",
       "      <td>83.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23493</th>\n",
       "      <td>23562</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>29.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23494</th>\n",
       "      <td>23563</td>\n",
       "      <td>269</td>\n",
       "      <td>2</td>\n",
       "      <td>58.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23495</th>\n",
       "      <td>23564</td>\n",
       "      <td>212</td>\n",
       "      <td>3</td>\n",
       "      <td>70.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23496</th>\n",
       "      <td>23565</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23497</th>\n",
       "      <td>23566</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>36.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23498</th>\n",
       "      <td>23567</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>20.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23499</th>\n",
       "      <td>23568</td>\n",
       "      <td>434</td>\n",
       "      <td>3</td>\n",
       "      <td>121.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23500</th>\n",
       "      <td>23569</td>\n",
       "      <td>462</td>\n",
       "      <td>1</td>\n",
       "      <td>25.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23501</th>\n",
       "      <td>23570</td>\n",
       "      <td>461</td>\n",
       "      <td>2</td>\n",
       "      <td>94.08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>23502 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       user_id    R   F       M\n",
       "0            1  545   1   11.77\n",
       "1            2  534   2   89.00\n",
       "2            3   33   6  156.46\n",
       "3            4  200   4  100.50\n",
       "4            5  178  11  385.61\n",
       "5            6  545   1   20.99\n",
       "6            7  100   3  264.67\n",
       "7            8   93   8  197.66\n",
       "8            9   22   3   95.85\n",
       "9           10  525   1   39.31\n",
       "10          11  130   4   58.55\n",
       "11          12  545   1   57.06\n",
       "12          13  545   1   72.94\n",
       "13          14  545   1   29.92\n",
       "14          15  545   1   52.87\n",
       "15          16  293   4   79.87\n",
       "16          17  545   1   73.22\n",
       "17          18  542   1   14.96\n",
       "18          19  385   2  175.12\n",
       "19          20  528   2  653.01\n",
       "20          21  533   2   75.11\n",
       "21          22  545   1   14.37\n",
       "22          23  545   1   24.74\n",
       "23          24  161   2   57.77\n",
       "24          25   22   8  137.53\n",
       "25          26  520   2  102.69\n",
       "26          27  534   2  135.87\n",
       "27          28  479   3   90.99\n",
       "28          29   65  12  435.81\n",
       "29          30  501   2   28.34\n",
       "...        ...  ...  ..     ...\n",
       "23472    23541  454   2   57.34\n",
       "23473    23542  462   1   77.43\n",
       "23474    23543  462   1   50.76\n",
       "23475    23544  157   3  134.63\n",
       "23476    23545  462   1   24.99\n",
       "23477    23546  462   1   13.97\n",
       "23478    23547  449   2   23.54\n",
       "23479    23548  462   1   23.54\n",
       "23480    23549  462   1   27.13\n",
       "23481    23550  462   1   25.28\n",
       "23482    23551  292   6  264.63\n",
       "23483    23552  453   2   49.38\n",
       "23484    23553  459   2   98.58\n",
       "23485    23554  149   2   36.37\n",
       "23486    23555   20   5  189.18\n",
       "23487    23556   23   7  203.00\n",
       "23488    23557  462   1   14.37\n",
       "23489    23558  125   4  145.60\n",
       "23490    23559  368   3  111.65\n",
       "23491    23560  462   1   18.36\n",
       "23492    23561   32   3   83.46\n",
       "23493    23562  462   1   29.33\n",
       "23494    23563  269   2   58.75\n",
       "23495    23564  212   3   70.01\n",
       "23496    23565  462   1   11.77\n",
       "23497    23566  462   1   36.00\n",
       "23498    23567  462   1   20.97\n",
       "23499    23568  434   3  121.70\n",
       "23500    23569  462   1   25.74\n",
       "23501    23570  461   2   94.08\n",
       "\n",
       "[23502 rows x 4 columns]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rfm = data.groupby(['user_id'],as_index=False).agg({\n",
    "    'order_interval':'min',\n",
    "    'order_products':'count',\n",
    "    'order_money':'sum'\n",
    "})\n",
    "#重命名列：最近一次订单r，订单频率f和订单总金额m\n",
    "rfm.columns=['user_id','R','F','M']\n",
    "rfm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T04:59:02.479077Z",
     "start_time": "2020-08-06T04:59:02.468081Z"
    }
   },
   "source": [
    "# 完成所有用户的RFM 值计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:20.580951Z",
     "start_time": "2020-08-06T06:45:20.544968Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>R</th>\n",
       "      <th>F</th>\n",
       "      <th>M</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.392662</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.922085</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.071532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>-1.841153</td>\n",
       "      <td>0.651130</td>\n",
       "      <td>0.208974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>-0.920074</td>\n",
       "      <td>0.224200</td>\n",
       "      <td>-0.023714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>-1.041413</td>\n",
       "      <td>1.718453</td>\n",
       "      <td>1.161803</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id         R         F         M\n",
       "0        1  0.982755 -0.416193 -0.392662\n",
       "1        2  0.922085 -0.202729 -0.071532\n",
       "2        3 -1.841153  0.651130  0.208974\n",
       "3        4 -0.920074  0.224200 -0.023714\n",
       "4        5 -1.041413  1.718453  1.161803"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rfm.R = (rfm.R-rfm.R.mean(axis=0))/(rfm.R.std(axis=0))\n",
    "rfm.F = (rfm.F-rfm.F.mean(axis=0))/(rfm.F.std(axis=0))\n",
    "rfm.M = (rfm.M-rfm.M.mean(axis=0))/(rfm.M.std(axis=0))\n",
    "rfm.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二 构建模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 客户聚类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 采用k-means聚类算法对客户数据进行客户分群,聚成5类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:23.870885Z",
     "start_time": "2020-08-06T06:45:23.866886Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.cluster import KMeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:24.481231Z",
     "start_time": "2020-08-06T06:45:24.477702Z"
    }
   },
   "outputs": [],
   "source": [
    "kmodels= KMeans(n_clusters=4)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T07:15:51.356429Z",
     "start_time": "2020-08-06T07:15:51.348432Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,\n",
       "    n_clusters=4, n_init=10, n_jobs=1, precompute_distances='auto',\n",
       "    random_state=None, tol=0.0001, verbose=0)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmodels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:26.829565Z",
     "start_time": "2020-08-06T06:45:26.042082Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,\n",
       "    n_clusters=4, n_init=10, n_jobs=1, precompute_distances='auto',\n",
       "    random_state=None, tol=0.0001, verbose=0)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmodels.fit(rfm.loc[:,['R','F','M']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 客户特征分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:28.523981Z",
     "start_time": "2020-08-06T06:45:28.518334Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 1, ..., 0, 0, 0])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmodels.labels_ # 查看样本对饮类别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:29.807525Z",
     "start_time": "2020-08-06T06:45:29.799051Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.6427928 , -0.33581688, -0.26298693],\n",
       "       [-1.31011532,  0.40910423,  0.24373823],\n",
       "       [-1.69402594,  3.28730767,  3.26110535],\n",
       "       [-1.75382519, 23.04712305, 23.18434875]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmodels.cluster_centers_ # 查看聚类中心"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:31.122106Z",
     "start_time": "2020-08-06T06:45:31.117167Z"
    }
   },
   "outputs": [],
   "source": [
    "rfm['labels']=kmodels.labels_  # 将类别列添加到客户RFm表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:33.868234Z",
     "start_time": "2020-08-06T06:45:33.844240Z"
    }
   },
   "outputs": [],
   "source": [
    "# 因为类别默认初始值为0,此处为了便于分析\n",
    "rfm['labels']=rfm['labels'].apply(lambda x:x+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T07:16:37.090501Z",
     "start_time": "2020-08-06T07:16:37.054522Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>R</th>\n",
       "      <th>F</th>\n",
       "      <th>M</th>\n",
       "      <th>labels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.392662</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.922085</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.071532</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>-1.841153</td>\n",
       "      <td>0.651130</td>\n",
       "      <td>0.208974</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>-0.920074</td>\n",
       "      <td>0.224200</td>\n",
       "      <td>-0.023714</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>-1.041413</td>\n",
       "      <td>1.718453</td>\n",
       "      <td>1.161803</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.354324</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>-1.471618</td>\n",
       "      <td>0.010736</td>\n",
       "      <td>0.658922</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>-1.510226</td>\n",
       "      <td>1.078059</td>\n",
       "      <td>0.380288</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>-1.901823</td>\n",
       "      <td>0.010736</td>\n",
       "      <td>-0.043049</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>0.872446</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.278148</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>11</td>\n",
       "      <td>-1.306155</td>\n",
       "      <td>0.224200</td>\n",
       "      <td>-0.198146</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.204342</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>13</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.138311</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>14</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.317193</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>15</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.221764</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>-0.407137</td>\n",
       "      <td>0.224200</td>\n",
       "      <td>-0.109495</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>17</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.137147</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18</td>\n",
       "      <td>0.966209</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.379398</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>19</td>\n",
       "      <td>0.100284</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>0.286564</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20</td>\n",
       "      <td>0.888993</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>2.273680</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>21</td>\n",
       "      <td>0.916570</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.129288</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>22</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.381851</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>23</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.338731</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>24</td>\n",
       "      <td>-1.135176</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.201389</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>25</td>\n",
       "      <td>-1.901823</td>\n",
       "      <td>1.078059</td>\n",
       "      <td>0.130261</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>26</td>\n",
       "      <td>0.844869</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.014607</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>27</td>\n",
       "      <td>0.922085</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>0.123358</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>28</td>\n",
       "      <td>0.618736</td>\n",
       "      <td>0.010736</td>\n",
       "      <td>-0.063257</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>29</td>\n",
       "      <td>-1.664659</td>\n",
       "      <td>1.931917</td>\n",
       "      <td>1.370540</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>30</td>\n",
       "      <td>0.740076</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.323762</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23472</th>\n",
       "      <td>23541</td>\n",
       "      <td>0.480850</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.203177</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23473</th>\n",
       "      <td>23542</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.119641</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23474</th>\n",
       "      <td>23543</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.230538</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23475</th>\n",
       "      <td>23544</td>\n",
       "      <td>-1.157238</td>\n",
       "      <td>0.010736</td>\n",
       "      <td>0.118202</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23476</th>\n",
       "      <td>23545</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.337692</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23477</th>\n",
       "      <td>23546</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.383514</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23478</th>\n",
       "      <td>23547</td>\n",
       "      <td>0.453272</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.343721</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23479</th>\n",
       "      <td>23548</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.343721</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23480</th>\n",
       "      <td>23549</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.328794</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23481</th>\n",
       "      <td>23550</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.336486</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23482</th>\n",
       "      <td>23551</td>\n",
       "      <td>-0.412653</td>\n",
       "      <td>0.651130</td>\n",
       "      <td>0.658756</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23483</th>\n",
       "      <td>23552</td>\n",
       "      <td>0.475334</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.236276</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23484</th>\n",
       "      <td>23553</td>\n",
       "      <td>0.508427</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.031697</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23485</th>\n",
       "      <td>23554</td>\n",
       "      <td>-1.201361</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.290373</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23486</th>\n",
       "      <td>23555</td>\n",
       "      <td>-1.912854</td>\n",
       "      <td>0.437665</td>\n",
       "      <td>0.345027</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23487</th>\n",
       "      <td>23556</td>\n",
       "      <td>-1.896308</td>\n",
       "      <td>0.864594</td>\n",
       "      <td>0.402492</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23488</th>\n",
       "      <td>23557</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.381851</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23489</th>\n",
       "      <td>23558</td>\n",
       "      <td>-1.333732</td>\n",
       "      <td>0.224200</td>\n",
       "      <td>0.163817</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23490</th>\n",
       "      <td>23559</td>\n",
       "      <td>0.006521</td>\n",
       "      <td>0.010736</td>\n",
       "      <td>0.022649</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23491</th>\n",
       "      <td>23560</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.365260</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23492</th>\n",
       "      <td>23561</td>\n",
       "      <td>-1.846669</td>\n",
       "      <td>0.010736</td>\n",
       "      <td>-0.094568</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23493</th>\n",
       "      <td>23562</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.319646</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23494</th>\n",
       "      <td>23563</td>\n",
       "      <td>-0.539508</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.197314</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23495</th>\n",
       "      <td>23564</td>\n",
       "      <td>-0.853888</td>\n",
       "      <td>0.010736</td>\n",
       "      <td>-0.150494</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23496</th>\n",
       "      <td>23565</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.392662</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23497</th>\n",
       "      <td>23566</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.291911</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23498</th>\n",
       "      <td>23567</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.354408</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23499</th>\n",
       "      <td>23568</td>\n",
       "      <td>0.370541</td>\n",
       "      <td>0.010736</td>\n",
       "      <td>0.064438</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23500</th>\n",
       "      <td>23569</td>\n",
       "      <td>0.524973</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.334573</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23501</th>\n",
       "      <td>23570</td>\n",
       "      <td>0.519458</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.050409</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>23502 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       user_id         R         F         M  labels\n",
       "0            1  0.982755 -0.416193 -0.392662       1\n",
       "1            2  0.922085 -0.202729 -0.071532       1\n",
       "2            3 -1.841153  0.651130  0.208974       2\n",
       "3            4 -0.920074  0.224200 -0.023714       2\n",
       "4            5 -1.041413  1.718453  1.161803       2\n",
       "5            6  0.982755 -0.416193 -0.354324       1\n",
       "6            7 -1.471618  0.010736  0.658922       2\n",
       "7            8 -1.510226  1.078059  0.380288       2\n",
       "8            9 -1.901823  0.010736 -0.043049       2\n",
       "9           10  0.872446 -0.416193 -0.278148       1\n",
       "10          11 -1.306155  0.224200 -0.198146       2\n",
       "11          12  0.982755 -0.416193 -0.204342       1\n",
       "12          13  0.982755 -0.416193 -0.138311       1\n",
       "13          14  0.982755 -0.416193 -0.317193       1\n",
       "14          15  0.982755 -0.416193 -0.221764       1\n",
       "15          16 -0.407137  0.224200 -0.109495       2\n",
       "16          17  0.982755 -0.416193 -0.137147       1\n",
       "17          18  0.966209 -0.416193 -0.379398       1\n",
       "18          19  0.100284 -0.202729  0.286564       1\n",
       "19          20  0.888993 -0.202729  2.273680       1\n",
       "20          21  0.916570 -0.202729 -0.129288       1\n",
       "21          22  0.982755 -0.416193 -0.381851       1\n",
       "22          23  0.982755 -0.416193 -0.338731       1\n",
       "23          24 -1.135176 -0.202729 -0.201389       2\n",
       "24          25 -1.901823  1.078059  0.130261       2\n",
       "25          26  0.844869 -0.202729 -0.014607       1\n",
       "26          27  0.922085 -0.202729  0.123358       1\n",
       "27          28  0.618736  0.010736 -0.063257       1\n",
       "28          29 -1.664659  1.931917  1.370540       2\n",
       "29          30  0.740076 -0.202729 -0.323762       1\n",
       "...        ...       ...       ...       ...     ...\n",
       "23472    23541  0.480850 -0.202729 -0.203177       1\n",
       "23473    23542  0.524973 -0.416193 -0.119641       1\n",
       "23474    23543  0.524973 -0.416193 -0.230538       1\n",
       "23475    23544 -1.157238  0.010736  0.118202       2\n",
       "23476    23545  0.524973 -0.416193 -0.337692       1\n",
       "23477    23546  0.524973 -0.416193 -0.383514       1\n",
       "23478    23547  0.453272 -0.202729 -0.343721       1\n",
       "23479    23548  0.524973 -0.416193 -0.343721       1\n",
       "23480    23549  0.524973 -0.416193 -0.328794       1\n",
       "23481    23550  0.524973 -0.416193 -0.336486       1\n",
       "23482    23551 -0.412653  0.651130  0.658756       2\n",
       "23483    23552  0.475334 -0.202729 -0.236276       1\n",
       "23484    23553  0.508427 -0.202729 -0.031697       1\n",
       "23485    23554 -1.201361 -0.202729 -0.290373       2\n",
       "23486    23555 -1.912854  0.437665  0.345027       2\n",
       "23487    23556 -1.896308  0.864594  0.402492       2\n",
       "23488    23557  0.524973 -0.416193 -0.381851       1\n",
       "23489    23558 -1.333732  0.224200  0.163817       2\n",
       "23490    23559  0.006521  0.010736  0.022649       1\n",
       "23491    23560  0.524973 -0.416193 -0.365260       1\n",
       "23492    23561 -1.846669  0.010736 -0.094568       2\n",
       "23493    23562  0.524973 -0.416193 -0.319646       1\n",
       "23494    23563 -0.539508 -0.202729 -0.197314       2\n",
       "23495    23564 -0.853888  0.010736 -0.150494       2\n",
       "23496    23565  0.524973 -0.416193 -0.392662       1\n",
       "23497    23566  0.524973 -0.416193 -0.291911       1\n",
       "23498    23567  0.524973 -0.416193 -0.354408       1\n",
       "23499    23568  0.370541  0.010736  0.064438       1\n",
       "23500    23569  0.524973 -0.416193 -0.334573       1\n",
       "23501    23570  0.519458 -0.202729 -0.050409       1\n",
       "\n",
       "[23502 rows x 5 columns]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rfm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:20:31.355689Z",
     "start_time": "2020-08-06T06:20:31.351695Z"
    }
   },
   "source": [
    "## 查看各类别客户人群数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:36.735137Z",
     "start_time": "2020-08-06T06:45:36.728139Z"
    }
   },
   "outputs": [],
   "source": [
    "label = rfm.groupby('labels')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T07:17:47.848881Z",
     "start_time": "2020-08-06T07:17:47.824103Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>R</th>\n",
       "      <th>F</th>\n",
       "      <th>M</th>\n",
       "      <th>labels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.392662</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.922085</td>\n",
       "      <td>-0.202729</td>\n",
       "      <td>-0.071532</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>-1.841153</td>\n",
       "      <td>0.651130</td>\n",
       "      <td>0.208974</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>-0.920074</td>\n",
       "      <td>0.224200</td>\n",
       "      <td>-0.023714</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>-1.041413</td>\n",
       "      <td>1.718453</td>\n",
       "      <td>1.161803</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.354324</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>-1.471618</td>\n",
       "      <td>0.010736</td>\n",
       "      <td>0.658922</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>-1.510226</td>\n",
       "      <td>1.078059</td>\n",
       "      <td>0.380288</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>0.872446</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.278148</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>0.982755</td>\n",
       "      <td>-0.416193</td>\n",
       "      <td>-0.204342</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>33</td>\n",
       "      <td>-1.421979</td>\n",
       "      <td>4.706957</td>\n",
       "      <td>3.905569</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>47</td>\n",
       "      <td>-1.890792</td>\n",
       "      <td>3.853099</td>\n",
       "      <td>1.337233</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>48</td>\n",
       "      <td>-1.929400</td>\n",
       "      <td>3.639634</td>\n",
       "      <td>2.260873</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>89</td>\n",
       "      <td>-1.912854</td>\n",
       "      <td>3.212705</td>\n",
       "      <td>1.936333</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>95</td>\n",
       "      <td>-1.990070</td>\n",
       "      <td>5.560815</td>\n",
       "      <td>5.859045</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>497</th>\n",
       "      <td>499</td>\n",
       "      <td>-1.973524</td>\n",
       "      <td>17.514832</td>\n",
       "      <td>16.454016</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3033</th>\n",
       "      <td>3049</td>\n",
       "      <td>-2.017647</td>\n",
       "      <td>22.851447</td>\n",
       "      <td>16.597928</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6541</th>\n",
       "      <td>6569</td>\n",
       "      <td>-1.984555</td>\n",
       "      <td>8.335855</td>\n",
       "      <td>20.215851</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7115</th>\n",
       "      <td>7145</td>\n",
       "      <td>-1.670174</td>\n",
       "      <td>21.143730</td>\n",
       "      <td>8.204714</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7562</th>\n",
       "      <td>7592</td>\n",
       "      <td>-2.017647</td>\n",
       "      <td>42.063260</td>\n",
       "      <td>57.464053</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      user_id         R          F          M  labels\n",
       "0           1  0.982755  -0.416193  -0.392662       1\n",
       "1           2  0.922085  -0.202729  -0.071532       1\n",
       "2           3 -1.841153   0.651130   0.208974       2\n",
       "3           4 -0.920074   0.224200  -0.023714       2\n",
       "4           5 -1.041413   1.718453   1.161803       2\n",
       "5           6  0.982755  -0.416193  -0.354324       1\n",
       "6           7 -1.471618   0.010736   0.658922       2\n",
       "7           8 -1.510226   1.078059   0.380288       2\n",
       "9          10  0.872446  -0.416193  -0.278148       1\n",
       "11         12  0.982755  -0.416193  -0.204342       1\n",
       "32         33 -1.421979   4.706957   3.905569       3\n",
       "46         47 -1.890792   3.853099   1.337233       3\n",
       "47         48 -1.929400   3.639634   2.260873       3\n",
       "88         89 -1.912854   3.212705   1.936333       3\n",
       "94         95 -1.990070   5.560815   5.859045       3\n",
       "497       499 -1.973524  17.514832  16.454016       4\n",
       "3033     3049 -2.017647  22.851447  16.597928       4\n",
       "6541     6569 -1.984555   8.335855  20.215851       4\n",
       "7115     7145 -1.670174  21.143730   8.204714       4\n",
       "7562     7592 -2.017647  42.063260  57.464053       4"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T06:45:38.226610Z",
     "start_time": "2020-08-06T06:45:38.216618Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "labels\n",
       "1    15903\n",
       "2     6907\n",
       "3      680\n",
       "4       12\n",
       "Name: labels, dtype: int64"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label['labels'].agg('count')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "经过上面的分析，得到了4类客户群体的特征，可根据用户的量级分为4类人群。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第1类人群，占比最多，达67.66%，属于一般客户或低价值客户，该类人群在RFM三个维度上都比较差，传统意义上是最后考虑发展该类客户，但由于在此案例中该类人群占比最多，说明客户整体消费频率与金额都不太理想，需要重新审视产品是否面临问题。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T07:12:06.465876Z",
     "start_time": "2020-08-06T07:12:06.458426Z"
    }
   },
   "source": [
    "第2类人群，占比次多，达29.38%，属于有潜力的一般客户，这类人群购买新进度、金额和频率都表现一般，考虑其也有一定的群体基础，因此可采取常规性的礼品兑换和赠送、购物社区活动、签到、免运费等手段维持并提升其消费状态。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第3类人群，占比较少，达0.03%，属于重要发展客户，该类人群在三个维度上都较优，表现出已经有一定忠诚度及用户粘性，主要目标是再提升购买频次和金额，可采取交叉销售、个性化推荐、组合优惠券等策略，提升其单词购买的订单金额及促进其重复购买。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第4类人群，占比最少，只有13人，属于忠诚的高价值客户，该类人群表现优异，可以考虑倾斜更多资源来保持该类客户，防止流失，例如设计VIP服务、转向服务、客服绿色通道，提供高价值附加服务的推荐等措施。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 整体分析思路"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此次分析主要使用RFM模型，依托K-Means聚类完成客户分群，并分析每类的客户特征，从而制定不同的运营策略。RFM模型的应用除此方法外，还可基于明确的业务特征，使用明确的业务数据来界定客户的分群标准"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 运营策略优化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除上一节最后针对每个客户群体提到的针对性策略，还可以组建会员体系，明确不同价值的会员享有不同的优惠与权益，提高客户的满意度和忠诚度。\n",
    "建立新客福利，有利于提升最大基数客群的购物体验，后期可根据第1类人群的聚类中心是否有变化来衡量新客福利的效果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-06T07:13:33.350097Z",
     "start_time": "2020-08-06T07:13:33.347098Z"
    }
   },
   "source": [
    "## 后续建议"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如有更多数据信息，可进行客户流失分析，针对目前的老客户进行分类预测。\n",
    "可每月运行一次，观察上述运营策略实施后的效果，对新增客户信息通过聚类中心进行判断，同时对本次新增客户的特征进行分析。如果增量数据的实际情况与判断结果差异大，需要业务部门重点关注。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
